﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using sqlserverextensions.Helper;


    public partial class UserDefinedFunctions
    {
        [Microsoft.SqlServer.Server.SqlFunction]
        public static SqlInt64 RailDigitSum(SqlInt64 value)
        {
            Int16 errSeverity = 16;
            SqlInt64 sum = 0;

            try
            {

                while (value != 0)
                {
                    sum += value % 10;
                    value /= 10;
                }

            }
            catch (Exception ex)
            {
                ExceptionHelper.EvaluateException(ex, errSeverity);
                return 0;
            }

            return sum;
        }
    };
